//
//  HNHappyWishTableView.m
//  XDEducation
//
//  Created by 星道 on 17/5/24.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNHappyWishTableView.h"
#import "HNHappyWishSmallTargetCell.h"
#import "HNHappWishBigTargetCell.h"

@implementation HNHappyWishTableView
{
    NSArray *_Arr;
    NSInteger _Index;
    UILabel *_TableViewHeader;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self)
    {
        self.delegate = self;
        self.dataSource = self;
        self.tableFooterView = [UIView new];
        self.tableHeaderView = [UIView new];
        [self registerNib:[UINib nibWithNibName:NSStringFromClass([HNHappyWishSmallTargetCell class]) bundle:nil] forCellReuseIdentifier:@"SmallCell"];
        [self registerNib:[UINib nibWithNibName:NSStringFromClass([HNHappWishBigTargetCell class]) bundle:nil] forCellReuseIdentifier:@"BigCell"];
        self.rowHeight = UITableViewAutomaticDimension;
        self.estimatedRowHeight = 300;
    }
    return self;
}


- (void)RefreshDataWithArr:(NSArray *)Arr Index:(NSInteger)Index
{
    _Arr = Arr;
    _Index = Index;

    if (Index == 0)
    {
        _TableViewHeader = nil;
        self.tableHeaderView = nil;
    }
    else
    {
        _TableViewHeader = [UILabel new];
        _TableViewHeader.frame = CGRectMake(0, 0, self.frame.size.width, 30);
        _TableViewHeader.backgroundColor = UIColor(140, 140, 140);
        _TableViewHeader.text = [NSString stringWithFormat:@"总共红花:%.1f朵",[[[Arr firstObject] flowers_total] floatValue]];
        _TableViewHeader.textAlignment = NSTextAlignmentCenter;
        _TableViewHeader.textColor = [UIColor whiteColor];
        self.tableHeaderView = _TableViewHeader;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self reloadData];
    });
}





#pragma mark - 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _Arr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_Index == 0)
    {
        HNHappyWishSmallTargetCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"SmallCell" forIndexPath:indexPath];
        Cell.SeeImg = ^(NSString *UrlStr)
        {
            if (self.SeeImg)
            {
                self.SeeImg (UrlStr);
            }
        };
        HNHappyWishSmallTarget *Model = _Arr[indexPath.section];
        Cell.Model = Model;
        return Cell;
    }
    else
    {
        HNHappWishBigTargetCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"BigCell" forIndexPath:indexPath];
        HNHappyWishBigTarget *Model = _Arr[indexPath.section];
        Cell.Model = Model;
        return Cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}



@end
